package com.yzq.config;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.yzq.utils.LongToStringSerializer;
import com.yzq.utils.StringToLongDeserializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

@Configuration
public class JacksonConfig {
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        SimpleModule module = new SimpleModule();
        // 注册自定义的序列化器
        module.addSerializer(Long.class, new LongToStringSerializer());
        // 注册自定义的反序列化器
        module.addDeserializer(Long.class, new StringToLongDeserializer());

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.getObjectMapper().registerModule(module);
        return converter;
    }
}
